//
//  VSFindOtherViewController.m
//  UI项目2.0
//
//  Created by dlios on 15/7/15.
//  Copyright (c) 2015年 星际科技. All rights reserved.
//

#import "VSFindOtherViewController.h"
#import "VSFindGiftWaterLayout.h"
#import "VSFindPaperCollectionViewCell.h"
#import "VSAFNetworkHandler.h"
#import "VSFindPaperModel.h"
#import "VSFindOtherPushViewController.h"
#import "MJRefresh.h"
@interface VSFindOtherViewController () <UICollectionViewDelegate, UICollectionViewDataSource, WaterLayoutDelegate>

@property (nonatomic, retain) NSMutableArray *PaperArr;
@property (nonatomic, retain) UICollectionView *PaperCollectionView;
@property (nonatomic, retain) VSFindPaperModel *paperModel;
@property (nonatomic, assign) NSInteger paperIndex;
@property (nonatomic, assign) NSInteger allIndex;
@end

@implementation VSFindOtherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.allIndex = 3;
    self.paperIndex = 4;
    
    self.PaperArr = [NSMutableArray array];
    
    [self paperGetDataUp];
    if (self.PaperCollectionView != nil) {
        [self getOtherData];
    }
    
}

/*
  女性 2 3
 
 */

- (void)createView {
    VSFindGiftWaterLayout *layout = [[[VSFindGiftWaterLayout alloc] init] autorelease];
    
    layout.sectionInset = UIEdgeInsetsMake(0.040 * SCREEN_WIDTH,0, 0, 0);
    layout.verticalSpacing = 0;
    layout.horizontalSpacing = 0;
    layout.lineCount = 3;
    
    self.PaperCollectionView = [[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout] autorelease];
    
    self.PaperCollectionView.backgroundColor = [UIColor lightGrayColor];
    [self.PaperCollectionView registerClass:[VSFindPaperCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.PaperCollectionView.delegate = self;
    self.PaperCollectionView.dataSource = self;
    
    [self.view addSubview:self.PaperCollectionView];
    
    if (self.PaperArr != nil) {
        self.PaperCollectionView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getOtherData)];
    }
    
    self.PaperCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(paperGetDataUp)];
    
    UIImageView *backGroundImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
    backGroundImage.image = [UIImage imageNamed:@"background001.png"];
    self.PaperCollectionView.backgroundView = backGroundImage;
}

- (void)paperGetDataUp
{
    
    NSString *str = [NSString stringWithFormat:@"%ld", self.allIndex];
    NSString *str1 = @"http://api.lovebizhi.com/iphone_v3.php?a=category&tid=";
    NSString *str2 =  [NSString stringWithFormat:@"&device=&uuid=883107D8DD8F4D55B30AFCAF47653854&mode=0&retina=0&client_id=1002&device_id=54277435&model_id=100&size_id=0&channel_id=19955&screen_width=750&screen_height=1334&bizhi_width=1080&bizhi_height=1920&version_code=52&language=zh-Hans&jailbreak=0&mac=FB26DA46-D85D-43DC-8A06-7820F69A0EBA&order=newest&color_id=3"];
    NSString *temp = [[str1 stringByAppendingString:str]stringByAppendingString:str2];
    [VSAFNetworkHandler GETwithAFNbyURL:temp completion:^(id result) {
        [self.PaperArr removeAllObjects];
        NSArray *tempArr = [result objectForKey:@"data"];
        for (NSDictionary *dic in tempArr) {
            self.paperModel = [VSFindPaperModel baseModelWithDic:dic];
            [self.PaperArr addObject:self.paperModel];
        }
        [self createView];
        [self.PaperCollectionView reloadData];
        [self.PaperCollectionView.header endRefreshing];
    }];
    
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return self.PaperArr.count;
}

- ( UICollectionViewCell *)collectionView:( UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath
{
    VSFindPaperCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    cell.paperModel = self.PaperArr[indexPath.item];
    return cell;
}

- (void)getOtherData
{
    
 

    NSString *str = [NSString stringWithFormat:@"%ld", self.allIndex];
    NSString *str1 = @"http://api.lovebizhi.com/iphone_v3.php?a=category&tid=";
    NSString *str2 =  [NSString stringWithFormat:@"&device=&uuid=883107D8DD8F4D55B30AFCAF47653854&mode=0&retina=0&client_id=1002&device_id=54277435&model_id=100&size_id=0&channel_id=19955&screen_width=750&screen_height=1334&bizhi_width=1080&bizhi_height=1920&version_code=52&language=zh-Hans&jailbreak=0&mac=FB26DA46-D85D-43DC-8A06-7820F69A0EBA&order=newest&color_id=%ld", self.paperIndex];
   NSString *temp = [[str1 stringByAppendingString:str]stringByAppendingString:str2];
   [VSAFNetworkHandler GETwithAFNbyURL:temp completion:^(id result) {
        NSArray *tempArr = [result objectForKey:@"data"];
        for (NSDictionary *dic in tempArr) {
           self.paperModel = [VSFindPaperModel baseModelWithDic:dic];
            [self.PaperArr addObject:self.paperModel];
        }
        if (self.PaperCollectionView == nil) {
            [self createView];
        }
       self.paperIndex += 1;
        
        [self.PaperCollectionView reloadData];
        [self.PaperCollectionView.footer endRefreshing];
    }];
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    VSFindOtherPushViewController *new = [[[VSFindOtherPushViewController alloc]init] autorelease];
    VSFindPaperModel *model = self.PaperArr[indexPath.item];
    new.image = [model.image objectForKey:@"vip_original"];
   [self presentViewController:new animated:YES completion:^{
       
   }];
    
}

- (CGSize)collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH / 3, 204);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
